//
//  UIBarButtonItem+XLExtension.m
//  DrugProphet
//
//  Created by 左学良 on 16/7/21.
//  Copyright © 2016年 左学良. All rights reserved.
//

#import "UIBarButtonItem+XLExtension.h"
#import "UIView+XLExtension.h"
@implementation UIBarButtonItem (XLExtension)

/**
 *  创建一个UIBarButtonItem
 *
 *  @param image     图片名字
 *  @param highImage 高亮图片名字
 *  @param target    target
 *  @param action    响应方法
 *
 *  @return 返回一个UIBarButtonItem
 */
+ (instancetype)itemWithImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action
{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
    button.zxl_size = button.currentBackgroundImage.size;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[self alloc] initWithCustomView:button];
}

+ (UIBarButtonItem *)itemWithImageName:(NSString *)imageName highImageName:(NSString *)highImageName target:(id)target action:(SEL)action
{
    UIButton *button = [[UIButton alloc] init];
    [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:highImageName] forState:UIControlStateHighlighted];
    button.timeInterval = 0.3;
    // 设置按钮的尺寸为背景图片的尺寸
    button.zxl_size = button.currentBackgroundImage.size;
    // 监听按钮点击
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (UIBarButtonItem *)itemInitWithImageName:(NSString *)imageName highImageName:(NSString *)highImageName target:(id)target action:(SEL)action
{
    return [self itemInitWithImageName:imageName highImageName:highImageName target:target action:action];
}


- (UIBarButtonItem *)itemWithTarget:(id)target WithTitle:(NSString *)title withColor:(UIColor *)color action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGSize size = [title sizeWithAttributes:@{NSFontAttributeName:xlFont(13)}];
    //! 这里需要根据内容大小来调整宽度
    button.frame = CGRectMake(0, 0, size.width <= 10 ? 35 : size.width + 10, 32);
//    button.titleLabel.textColor = xlSecTextColor;
    button.titleLabel.font = xlFont(15);
    button.titleLabel.textAlignment = NSTextAlignmentRight;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    return item;
    
}
+ (UIBarButtonItem *)addTarget:(id)target WithTitle:(NSString *)title withColor:(UIColor *)color action:(SEL)action
{
    return [[self alloc]itemWithTarget:target WithTitle:title withColor:color action:action];
}


- (UIBarButtonItem *)itemInitWithImageName:(NSString *)imageName withTitle:(NSString *)title  highImageName:(NSString *)highImageName withHighTitle:(NSString *)highTitle target:(id)target action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGSize size = [title sizeWithAttributes:@{NSFontAttributeName:xlFont(13)}];
    //! 这里需要根据内容大小来调整宽度
    button.frame = CGRectMake(0, 8, size.width <= 10 ? 50 : size.width + 20, 32);
//    button.titleLabel.textColor = xlSecTextColor;
    button.titleLabel.font = xlFont(13);
    button.titleLabel.textAlignment = NSTextAlignmentRight;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:highImageName] forState:UIControlStateHighlighted];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    //    button.layer.cornerRadius = 5;
    //    button.layer.borderColor =[UIColor lightGrayColor].CGColor;
    //    button.layer.borderWidth =0.5;
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    return item;
}
+ (UIBarButtonItem *)itemWithImageName:(NSString *)imageName withTitle:(NSString *)title  highImageName:(NSString *)highImageName withHighTitle:(NSString *)highTitle target:(id)target action:(SEL)action
{
    return [[self alloc]itemInitWithImageName:imageName withTitle:title highImageName:highImageName withHighTitle:highTitle target:target action:action];
}




@end
